package tx.hw.demo01;

/**
 * @author lwj
 * @date 2025-03-13
 * @vesion 1.0.0
 */
public class Boy {
    /**
     * 水果数量
     */
    private int fruitNum;

    // 构造方法
    public Boy() {}
    public Boy(int fruitNum) {
        this.fruitNum = fruitNum;
    }

    public void pick() {
        synchronized (MyRunnable.LOCK){
            while(true){
                // try {
                //     Thread.sleep(1000);
                // } catch (InterruptedException e) {
                //     throw new RuntimeException(e);
                // }
                System.out.println("我现在去摘水果...");
                fruitNum += 1;
                if(fruitNum == 5){
                    System.out.println("我摘完了，快来吃");

                    // 唤醒女孩
                    MyRunnable.LOCK.notifyAll();
                    break;
                }
            }
            try {
                // 线程等待
                MyRunnable.LOCK.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
